package com.zjl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zjl.mapper.EmpMapper;
import com.zjl.pojo.Emp;
import com.zjl.pojo.PageBean;
import com.zjl.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName EmpServiceImpl
 * @Description 员工表逻辑实现层
 * @Author ZJL
 * @Date 2023/10/26 18:25
 * @Version 1.0
 **/
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    /**
     * 调用mapper接口查询总记录数
     * 调用mapper接口获取数据列表
     * 封装PageBean对象，返回
     * @param page
     * @param pageSize
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //1.设置分页参数
        //2.执行分页查询
        //3.获取分页结果 封装PageBean
        PageHelper.startPage(page,pageSize);
        List<Emp> empList = empMapper.list(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) empList;
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public Integer delete(List<Integer> ids) {
        Integer result = empMapper.delete(ids);
        return result;
    }

    @Override
    public Integer add(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        return empMapper.insert(emp);
    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.findById(id);
    }

    @Override
    public Integer update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());  //更新修改时间为当前时间
        Integer result  = empMapper.update(emp);
        return result;
    }


    @Override
    public Emp login(Emp emp) {
        //调用dao层功能：登录
        Emp loginEmp = empMapper.getByUsernameAndPassword(emp);

        //返回查询结果给Controller
        return loginEmp;
}
}
